package com.wgchao.identityrightsmanagement.application.service;

import com.wgchao.identityrightsmanagement.domain.organization.model.*;
import com.wgchao.identityrightsmanagement.domain.organization.repository.AgencyRepository;
import com.wgchao.identityrightsmanagement.infrastructure.exception.ObjectNotExistException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName: AgencyApplicationService
 * @author: wgchao
 * @createTime: 2020/6/8 9:46 AM
 * @Description: 应用服务-机构
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class AgencyApplicationService {
    @Autowired
    private AgencyRepository agencyRepository;

    /**
     * 创建新的机构
     * @param agencyCreator 机构信息
     * @param operator 操作员账户名
     */
    public void addNewAgency(AgencyCreator agencyCreator, String operator){
        String agencyCode = agencyRepository.generateCode();
        Agency agency = Agency.createAgency(agencyCode, agencyCreator);
        agencyRepository.add(agency, operator);
    }

    /**
     * 修改机构基础信息
     * @param agencyCode
     * @param agencyUpdator
     * @param operator
     */
    public void modifyAgencyInfo(String agencyCode, AgencyUpdator agencyUpdator, String operator){
        Agency agency = agencyRepository.findById(agencyCode);
        if(agency == null){
            throw new ObjectNotExistException("机构不存在");
        }
        agency.modifyAgencyInfo(agencyUpdator);
        agencyRepository.add(agency, operator);
    }

    /**
     * 删除机构
     * @param agencyCode
     * @param operator
     */
    public void removeAgency(String agencyCode, String operator){
        Agency agency = agencyRepository.findById(agencyCode);
        if(agency == null){
            throw new ObjectNotExistException("机构不存在");
        }
        agencyRepository.remove(agency, operator);
    }

    /**
     * 批量删除机构
     * @param agencyCodes
     * @param operator
     */
    public void removeAgencyList(List<String> agencyCodes, String operator){
        for(String agencyCode : agencyCodes){
            removeAgency(agencyCode, operator);
        }
    }

    /**
     * 停用机构
     * @param agencyCode
     * @param operator
     */
    public void disable(String agencyCode, String operator){
        Agency agency = agencyRepository.findById(agencyCode);
        if(agency == null){
            throw new ObjectNotExistException("机构不存在");
        }
        agency.disable();
        agencyRepository.add(agency, operator);
    }

    /**
     * 启用机构
     * @param agencyCode
     * @param operator
     */
    public void regain(String agencyCode, String operator){
        Agency agency = agencyRepository.findById(agencyCode);
        if(agency == null){
            throw new ObjectNotExistException("机构不存在");
        }
        agency.regain();
        agencyRepository.add(agency, operator);
    }

}
